package com.android.vestel.photoroid;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PicasaAlbumSelectionActivity extends Activity {
	int selectedItemPosition;
	Bundle b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picasa_album);
        // set selectedItemPositon to -1 for error checking
        selectedItemPosition = -1;
        
        final ListView lw = (ListView) findViewById(R.id.P_listView);
        
        b = getIntent().getExtras();
        final ArrayList<String> albumNames = b.getStringArrayList("albumNames");
        final ArrayList<String> albumIDs = b.getStringArrayList("albumIDs");
        // clear bundle and prepare for 
        
        if(albumNames.size() == 0){ // no albums exist on the account
        	albumNames.add("No albums");
        	albumIDs.add("-1");
        }
        
        lw.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,albumNames));
        
        lw.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				selectedItemPosition = arg2;
				Log.e("PicasaAlbmSelectionActivity","Item " + albumNames.get(arg2) + " is clicked.");
				String value = "-1"; // the value that is going to be sent with the intent.
				if(!albumNames.get(selectedItemPosition).equals("No albums") && !albumIDs.get(selectedItemPosition).equals("-1")){
					value = albumIDs.get(selectedItemPosition);
				}
				// put album id into an intent then go back
				b.clear();
				b.putString("selectedAlbumID", value);
				Intent goBackToPicasaForm = new Intent();
				goBackToPicasaForm.putExtras(b);
				setResult(0, goBackToPicasaForm);
				finish();
			}
        	
		});
    }
    @Override
    public void onBackPressed() {
    	super.onBackPressed();
    	Log.e("PicasaAlbmSelectionActivity","Back pressed.");
		finish();
    }
}
